package org.s3.project.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.s3.project.entity.Comment;
import org.s3.project.entity.Dish;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.List;

/**
 * @author cfx
 * @version 1.0
 * @Description
 * @date 2025/2/9 9:40
 */
@Mapper
@Repository
public interface DishMapper extends BaseMapper<Dish> {
    //查询展示
    @Select("select \n" +
            "    d.id,\n" +
            "    d.name,\n" +
            "    d.category_id,\n" +
            "    d.price,\n" +
            "    d.image,\n" +
            "    d.description,\n" +
            "    d.status,\n" +
            "    d.create_time,\n" +
            "    d.update_time,\n" +
            "    d.create_user,\n" +
            "    d.update_user,\n" +
            "    d.inventory,\n" +
            "    m.longitude,\n" +
            "    m.latitude,\n" +
            "    m.distance\n" +
            "from \n" +
            "    dish d\n" +
            "\n" +
            "join \n" +
            "    merchant_dish md on d.id = md.dish_id\n" +
            "\n" +
            "join \n" +
            "    merchant m on md.merchant_id = m.id;")
    List<Dish> getDishSelect();

    //只显示评分最高的三个菜品
    @Select("select \n" +
            "    d.id,\n" +
            "    d.name,\n" +
            "    d.category_id,\n" +
            "    d.price,\n" +
            "    d.image,\n" +
            "    d.description,\n" +
            "    d.status,\n" +
            "    d.create_time,\n" +
            "    d.update_time,\n" +
            "    d.create_user,\n" +
            "    d.update_user,\n" +
            "    d.inventory,\n" +
            "    m.longitude,\n" +
            "    m.latitude,\n" +
            "    m.distance\n" +
            "from \n" +
            "    dish d\n" +
            "\n" +
            "join \n" +
            "    merchant_dish md on d.id = md.dish_id\n" +
            "\n" +
            "join \n" +
            "    merchant m on md.merchant_id = m.id;")
    List<Dish> getDishSelectByInventory();

    //通过商家id查询菜品
    @Select(" select d.id, d.name,d.price,d.image,d.description,d.create_time,d.update_time,d.create_user,d.update_user,d.inventory,m.image\n" +
            "from dish d,merchant_dish md,merchant m\n" +
            "where md.merchant_id=m.id and md.dish_id=d.id and merchant_id=#{id}")
    List<Dish> selectDishByMerchant(int id);

    @Select("select d.id, d.name, d.category_id, d.price, d.image, d.description, d.status, d.create_time, " +
            "d.update_time, d.create_user, d.update_user, d.inventory, m.longitude, m.latitude, m.distance " +
            "from dish d join merchant_dish md on d.id = md.dish_id join merchant m on md.merchant_id = m.id WHERE d.category_id =#{id} AND d.status=1 AND m.opening_status =1\n")
    List<Dish> getDishByCategoryId(int id);

    //详情
    @Select("select id,name,category_id,price,image,description,status,create_time,update_time,create_user,update_user,inventory \n" +
            "from dish\n" +
            "where id=#{id}")
    Dish getDishId(int id);
    //通过菜品id查询商家
    List<Dish> selectBusinessId(@Param("id") int id,@Param("category_id")int category_id);
    // 查询商家所有菜品 int category_id);
    // 查询商家所有菜品
//    @Select("select id,name,status,price,inventory from dish  where category_id=#{businessId}")
//    List<Dish> selectAllByBusinessId(Integer businessId);
    // 查询商家所有菜品
    @Select("select d.id,d.name,d.status,d.price,d.inventory from dish d,category c,merchant m  \n" +
            "where d.category_id=c.id and c.merchant_id=m.id and m.opening_status=1 and category_id=#{businessId}")
    List<Dish> selectAllByBusinessId(Integer businessId);
    //修改
    @Update("update dish set name = #{name},price = #{price},status = #{status},inventory = #{inventory} where id = #{id}")
    int updateDish(Dish dish);
    //新增
    @Insert("insert into dish(name,category_id,price,status,inventory) values(#{name},#{category_id},#{price},#{status},#{inventory})")
    int insertDish(Dish dish);

    /**
     * 根据商家id查询菜品(与刘家辉的略有不同)
     *
     * @param id 商家id
     * @return 菜品列表
     */
    List<Dish> selectDishByMerchants(@Param("id") Integer id, @Param("Dname") String Dname, @Param("create_time") String create_time, @Param("create_timeOne") String create_timeOne, @Param("status") Integer status);


    @Update("update dish set status = #{status},update_user=#{update_user},update_time=#{update_time} where id = #{id}")
    int updateDishStatus(Dish dish);

    /**
     * 批量修改菜品状态
     *
     * @param ids  菜品id数组
     * @param dish 菜品对象
     * @return 修改结果
     */
    int updateDishStatusIds(@Param("ids") int[] ids, @Param("dish") Dish dish);

    /**
     * 分页查询菜品信息
     *
     * @param Dname          菜品名称
     * @param Mname          商家名称
     * @param create_time    创建时间
     * @param create_timeOne 创建时间
     * @return
     */
    List<Dish> selectDishByMerchantChecks(@Param("Dname") String Dname, @Param("Mname") String Mname, @Param("create_time") String create_time, @Param("create_timeOne") String create_timeOne);

    //模糊查询菜品
    @Select("select d.id, d.name, d.category_id, d.price, d.image, d.description, d.status, d.create_time, d.update_time, d.create_user, d.update_user, d.inventory, m.longitude, m.latitude, m.distance from dish d join merchant_dish md on d.id = md.dish_id join merchant m on md.merchant_id = m.id where d.name like concat('%',#{name},'%')")
    List<Dish> selectDishByName(String name);
}
